package system;

import common.AutotestUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

public class LoginPage extends AutotestUtils {

    // 登录页面url
    static String url = "http://115.190.86.135:10030/oj/login";

    public LoginPage() {
        super(url);
    }

    // 查看页面是否加载成功
    public void loginPageRight(){
      assert   driver.findElement(By.cssSelector("#app > div.login-page > div > div.logo-box > div > div.sys-name")).getText().equals("YQ-OJ后台管理");
    }

    // 检查登录功能 -- 成功登录
    public void loginSuccess() throws InterruptedException {
        // 先清空输入框
       driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).clear();
        // 输入正确用户名+密码+
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).sendKeys("zhangsan");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).sendKeys("123456");
        // 登录
        driver.findElement(By.cssSelector("#app > div.login-page > div > div.form-box > div.submit-box")).click();

        Thread.sleep(1000);
        // 判断当前是否是成功登录的主页

       assert  driver.findElement(By.cssSelector("#app > section > main > div.left > aside > ul > li:nth-child(1) > span")).getText().equals("用户管理");
        // 退出


    }



    // 检查登录功能 -- 异常登录
    public void loginFail() throws InterruptedException, IOException {

        // 先清空输入框
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).clear();
        // 输入正确用户名+密码+
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).sendKeys("zhangsan1");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).sendKeys("123456");
        // 登录
        driver.findElement(By.cssSelector("#app > div.login-page > div > div.form-box > div.submit-box")).click();

        Thread.sleep(1000);
        // 判断当前是否是成功登录的主页

        assert  driver.findElement(By.cssSelector("#app > section > main > div.left > aside > ul > li:nth-child(1) > span")).getText().equals("用户管理");
        // 退出


        driver.quit();
    }
    // 检查登录功能 -- 异常登录 密码错误
    public void loginFail2() throws InterruptedException, IOException {

        // 先清空输入框
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).clear();
        // 输入正确用户名+密码+
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).sendKeys("zhangsan");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).sendKeys("1234561");
        // 登录
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#app > div.login-page > div > div.form-box > div.submit-box")).click();

        Thread.sleep(3000);
        // 判断当前是否是成功登录的主页

        assert  driver.findElement(By.cssSelector("#app > section > main > div.left > aside > ul > li:nth-child(1) > span")).getText().equals("用户管理");
        // 退出

        driver.quit();
    }


    // 用户管理
    // 拉黑用户 , 解禁用户
    public void  userChange() throws InterruptedException {
        driver.findElement(By.cssSelector("#app > section > main > div.left > aside > ul > li:nth-child(1)")).click();
        Thread.sleep(2000);
        // 拉黑用户
        driver.findElement(By.xpath("//*[@id=\"app\"]/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[10]/div/button")).click();
        Thread.sleep(1000);

        // 解禁
        Thread.sleep(500);
        driver.findElement(By.xpath("//*[@id=\"app\"]/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[10]/div/button")).click();
        Thread.sleep(500);
        String text2=  driver.findElement(By.xpath("//*[@id=\"app\"]/section/main/div[2]/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[10]/div/button/span")).getText();
        assert "拉黑".equals(text2) ;

        driver.quit();
    }


}
